<?php

class commonController extends AppController
{

    public function __construct()
    {


        
    }


    /** 验证必填字段
     * @param array $PosData    接收数组
     * @param array $duage  验证字段
     * @return bool
     */
    public static function Verify(array $PosData,array $duage,$file,$version){
        $log = new Logging();
        $log->lfile($file);
        $t = '';
        foreach ($duage as $k=>$p){
//            if(!strlen(trim($PosData[$k])) > 0){
//                $t = $k.'  不存在或为空';
//                break;
//            }

            if(!isset($PosData[$k]) || !$PosData[$k]){  # 非0
                $t = $k.'  不存在或为空';
                break;
            }


            if($p == 'check'){
                $checkCode = md5('shjieta');
                $checkCode = strtoupper(substr($checkCode, 0, 16));
                if ($checkCode !== $PosData['check_code']) {
                    $t = $k.'  校验码错误';
                    break;
                }
            }

            if($p == 'version'){
                if ($PosData[$k] !== $version) {
                    $t = $k.'  未知版本或字符类型有误';
                    break;
                }
            }

            if($p == 'phone'){
                if (!preg_match("/^((1))\d{10}$/", $PosData[$k])) {
                    $t = $k.'  手机号有误';
                    break;
                }
            }
            if($p == 'int'){
                if (!is_int($PosData[$k])) {
                    $t = $k.'  字符类型有误,必须为整型';
                    break;
                }
            }
            if($p == 'string'){
                if (!is_string($PosData[$k])) {
                    $t = $k.'  字符类型有误,必须为字符串';
                    break;
                }
            }
            if($p == 'double'){
                if (!is_float($PosData[$k])) {
                    $t = $k.'  字符类型有误,必须为double型';
                    break;
                }
            }
            if($p == 'NotNull'){
                if (empty($PosData[$k]) || is_null($PosData[$k])) {
                    $t = $k.'  不可空';
                    break;
                }
            }
            if($p == 'array'){
                if (!is_array($PosData[$k]) || count((array)$PosData[$k]) <= 0) {
                    $t = $k.'  格式错误';
                    break;
                }
            }
            if($p == 'id_card'){
                if (empty($PosData[$k]) || is_null($PosData[$k])) {
                    $t = $k.'  不可空';
                    break;
                }else{
                    $bool = self::is_idcard($PosData[$k]);
                    if($bool == false){
                        $t = $k.'  身份证号码有误';
                        break;
                    }
                }
            }
        }
        if($t){
            self::WriteResponse($file,0,$t,'');
        } else{
            return true;
        }
    }

    /**
     * 判断是否 身份证号码，包含尾数为英文 检索19xx年后
     * @param $id
     * @return bool
     */
    public static function is_idcard( $id )
    {
        $id = strtoupper($id);
        $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
        $arr_split = array();
        if(!preg_match($regx, $id))
        {
            return FALSE;
        }
        if(15==strlen($id))
        {
            $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";

            @preg_match($regx, $id, $arr_split);
            $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
            if(!strtotime($dtm_birth))
            {
                return FALSE;
            } else {
                return TRUE;
            }
        }
        else
        {
            $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
            @preg_match($regx, $id, $arr_split);
            $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
            if(!strtotime($dtm_birth))
            {
                return FALSE;
            }
            else
            {
                $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                $sign = 0;
                for ( $i = 0; $i < 17; $i++ )
                {
                    $b = (int) $id{$i};
                    $w = $arr_int[$i];
                    $sign += $b * $w;
                }
                $n = $sign % 11;
                $val_num = $arr_ch[$n];
                if ($val_num != substr($id,17, 1))
                {
                    return FALSE;
                }
                else
                {
                    return TRUE;
                }
            }
        }

    }



    /**
     * @param $file 文件名称
     * @param $GetData  接收数据包
        $log->lfile($file);
     */
    public static function WriteReception($file,$GetData){
        $log = new Logging();
        $log->lfile($file);
        $log->lwrite(' reception：' . $GetData );
    }

    /**
     * @param $file 文件名称
     * @param $errcode 代码
     * @param $errormsg 错误信息
     * @param $data 发送数据
     */
    public static function WriteResponse($file,$errcode,$errormsg,$data){
        $log = new Logging();
        $log->lfile($file);
        $data = self::clearNull($data);
        $resule = json_encode(['errcode' => $errcode, 'errormsg' => $errormsg,'data'=>$data],JSON_UNESCAPED_UNICODE);
        $log->lwrite(' response：' . $resule );
        echo $resule;exit;
    }



    public static function clearNull($data)
    {
        if (is_array($data)) {
            foreach ($data as $k => $v) {
                if (is_array($v)) {
                    $data[$k] = self::clearNull($v);
                } else {
                    if ($v=='' || is_null($v)) {

                        $data[$k] = '';
                    }
                }
            }
        }

        return $data;
    }

}

?>
